/*
 * @Author: C.
 * @Date: 2023-04-10 13:26:23
 */
import { useTable, BasicTableProps, BasicColumn } from '/@/components/Table';
import type { DynamicProps } from '/#/utils';
import { useI18n } from '/@/hooks/web/useI18n';
type Props = Partial<DynamicProps<BasicTableProps>>;
const { t } = useI18n();
const defaultActionColumn: BasicColumn = {
  width: 100,
  title: t('common.action'),
  dataIndex: 'action',
  fixed: 'right',
};
export function useAppTable(props: Props): ReturnType<typeof useTable> {
  const baseProps: Props = {
    rowKey: 'BillId',
    canResize: true,
    useSearchForm: true,
    showTableSetting: true,
    ...props,
  };
  if (Reflect.has(props, 'formConfig')) {
    baseProps.formConfig = {
      baseColProps: {
        span: 8,
      },
      rowProps: {
        gutter: 20,
      },
      submitOnChange: true,
      fieldMapToTime: [['DatePeriod', ['StartDate', 'EndDate']]],
      ...props.formConfig,
    };
  }
  if (Reflect.has(props, 'actionColumn')) {
    baseProps.actionColumn = {
      ...defaultActionColumn,
      ...props.actionColumn,
    };
  }
  return useTable(baseProps);
}
export function useAppEditTable(props: Props): ReturnType<typeof useTable> {
  const { t } = useI18n();
  const baseProps: Props = {
    columns: [],
    dataSource: [],
    showTableSetting: true,
    scroll: { y: 500 },
    rowKey: 'ItemId',
    pagination: {
      pageSize: 10,
    },
    inset: true,
    // 开启键盘导航
    editKeyboard: true,
    tableSetting: {
      import: false,
      export: true,
      setting: false,
      size: false,
      fullScreen: false,
      redo: false,
    },
    actionColumn: {
      ...defaultActionColumn,
    },
    ...props,
    title: ({ rowCount }) =>
      (props.title || t('common.editDetails')) + (rowCount == 0 ? '' : ` ( ${rowCount} )`),
  };
  if (Reflect.has(props, 'tableSetting')) {
    baseProps.tableSetting = {
      import: false,
      export: true,
      setting: false,
      size: false,
      fullScreen: false,
      redo: false,
      ...props.tableSetting,
    };
  }
  if (Reflect.has(props, 'actionColumn')) {
    baseProps.actionColumn = {
      ...defaultActionColumn,
      ...props.actionColumn,
    };
  }
  return useTable(baseProps);
}

export function useAppDetailsTable(props: Props): ReturnType<typeof useTable> {
  // const { t } = useI18n();
  const baseProps: Props = {
    columns: [],
    dataSource: [],
    showTableSetting: true,
    rowKey: 'ItemId',
    // 开启键盘导航
    editKeyboard: true,
    pagination: {
      pageSize: 10,
    },
    tableSetting: {
      import: false,
      export: true,
      setting: false,
      size: false,
      fullScreen: false,
      redo: false,
    },
    ...props,
    title: ({ rowCount }) =>
      (props.title || t('common.editDetails')) + (rowCount == 0 ? '' : ` ( ${rowCount} )`),
  };
  if (Reflect.has(props, 'tableSetting')) {
    baseProps.tableSetting = {
      import: false,
      export: true,
      setting: false,
      size: false,
      fullScreen: false,
      redo: false,
      ...props.tableSetting,
    };
  }
  if (Reflect.has(props, 'actionColumn')) {
    baseProps.actionColumn = {
      ...defaultActionColumn,
      ...props.actionColumn,
    };
  }
  return useTable(baseProps);
}
